package logic

import (
	"context"
	"github.com/pkg/errors"
	"im_sys/apps/user/models"

	"im_sys/apps/user/rpc/internal/svc"
	"im_sys/apps/user/rpc/user"

	"github.com/zeromicro/go-zero/core/logx"
)

var ErrUserNotFound = errors.New("用户不存在")

type GetUserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserInfoLogic) GetUserInfo(in *user.GetUserInfoReq) (*user.GetUserInfoResp, error) {
	// todo: add your logic here and delete this line
	result, err := l.svcCtx.UserModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if errors.Is(err, models.ErrNotFound) {
			return nil, ErrUserNotFound
		}
		return nil, err
	}
	return &user.GetUserInfoResp{
		User: &user.UserEntity{
			Id:       result.Id,
			Phone:    result.Phone,
			Nickname: result.Nickname,
			Avatar:   result.Avatar,
			Sex:      result.Sex.Int64,
			Status:   result.Status.Int64,
		},
	}, nil
}
